//
//  CYPopBoxView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/10/10.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYPopBoxView.h"
@interface CYBoxModel()

@end

@implementation CYBoxModel
- (NSString *)title{
    if (!_title) {
        _title = @"个人消息";
    }
    return _title;
}
@end

@interface CYPopBoxView (){
    CGSize _imageSize;
    
}
@property (nonatomic, strong) CYLabel *titleLBL;
@property (nonatomic, strong) CYLabel *contentsLBL;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) CYBoxModel *model;
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation CYPopBoxView
- (instancetype)init{
    self = [super init];
    if (self) {
        self.layer.cornerRadius = 5.0;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor whiteColor];
        self.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH - 40, 144);
    }
    return self;
}

static CYPopBoxView *_popBoxView;
static dispatch_once_t onceToken;
+ (instancetype)initWithModel:(CYBoxModel *)model{
    if (!_popBoxView) {
        dispatch_once(&onceToken, ^{
            _popBoxView = [[CYPopBoxView alloc] init];
            _popBoxView.model = model;
        });
    }
    return _popBoxView;
}

- (void)addWindow{
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window makeKeyAndVisible];
    
    CYView *alphaView  = [[CYView alloc]initWithColor:[UIColor blackColor]];
    alphaView.tag = 301;
    alphaView.alpha = 0.3;
    [window addSubview:alphaView];
    
    [alphaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0));
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapMethod)];
    [alphaView addGestureRecognizer:tap];
    
    
    [window addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(window);
        make.left.equalTo(window).with.offset(20);
        make.right.equalTo(window).with.offset(-20);
        make.height.mas_equalTo(self.bounds.size.height);
    }];
}
- (void)setModel:(CYBoxModel *)model{
    if (_model != model) {
        _model = model;
        
        [self addWindow];
        self.titleLBL.text = _model.title;
        if (_model.contents)
            self.contentsLBL.text = _model.contents;
        if (_model.address)
            [CYHttps loadingWithFile:_model.address complete:^(UIImage * data) {
                _imageSize = data.size;
                self.imageView.image = data;
            }];
    }
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:_model.title size:14 textcolor:[UIColor whiteColor]];
        _titleLBL.backgroundColor = THEME_COLOR;
        _titleLBL.textAlignment = NSTextAlignmentCenter;
        _titleLBL.bounds = CGRectMake(0.0, 0.0, _titleLBL.bounds.size.width, 44);
        [self addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.height.mas_equalTo(_titleLBL.bounds.size.height);
        }];
    }
    return _titleLBL;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        [self addSubview:_scrollView];
        
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(44 + 5, 5, 5, 5));
        }];
    }
    return _scrollView;
}

- (CYLabel *)contentsLBL{
    if (!_contentsLBL) {
        _contentsLBL = [[CYLabel alloc]initWithText:_model.contents size:16 textcolor:TEXT_BLACK_COLOR];
        _contentsLBL.textAlignment = NSTextAlignmentCenter;
        _contentsLBL.numberOfLines = 0;
        [self.scrollView addSubview:_contentsLBL];
        
        CGFloat height = [_contentsLBL autoSizeWithWidth:SCREEN_WIDTH - self.bounds.size.width - 20].height;
        [_contentsLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(10);
            make.right.equalTo(self).with.offset(-10);
            if (height > self.bounds.size.height - _titleLBL.bounds.size.height) {
                make.top.equalTo(self.scrollView);
                make.bottom.equalTo(self.scrollView);
            }
            else{
                make.centerY.equalTo(self.scrollView);
                make.size.mas_equalTo(_contentsLBL.bounds.size);
            }
        }];
        
        if (height > self.bounds.size.height - _titleLBL.bounds.size.height )
            [self updateBounds:(CGFloat)height + _titleLBL.bounds.size.height];
    }
    return _contentsLBL;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
        [self addSubview:_imageView];
        
        CGFloat height = _imageSize.height * 1.0 / _imageSize.width * (self.bounds.size.width - 10);
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(5);
            make.right.equalTo(self).with.offset(-5);
            if (height > self.bounds.size.height - _titleLBL.bounds.size.height) {
                make.top.equalTo(self).with.offset(5);
                make.bottom.equalTo(self).with.offset(5);
            }
            else{
                make.centerY.equalTo(self).with.offset(22);
                make.height.mas_equalTo(_imageSize.height * 1.0 / _imageSize.width * (self.bounds.size.width - 10));
            }
        }];
        
        if (height > self.bounds.size.height - _titleLBL.bounds.size.height )
            [self updateBounds:(CGFloat)height + _titleLBL.bounds.size.height];
    }
    return _imageView;
}

- (void)updateBounds:(CGFloat)newH{
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        if (newH < 300)
            make.height.mas_equalTo(newH);
        else
            make.height.mas_equalTo(300);
    }];
    
}
- (void)tapMethod{
    
    [self removeFromSuperview];
    CYView *alphaView = [[UIApplication sharedApplication].delegate.window viewWithTag:301];
    [alphaView removeFromSuperview];
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
